package hflyeer.o2o.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import hflyeer.o2o.biz.service.ProductCategoryService;
import hflyeer.o2o.model.entity.ProductCategory;

@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryController {

	// json和java bean互转
	private ObjectMapper mapper = new ObjectMapper();

	@Autowired
	private ProductCategoryService productCategoryService;

	/**
	 * 获取商品分类列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/productcategorylist", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getProductCategoryList(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<>();

		String shopIdStr = request.getParameter("shopId");
		// 前台传入参数不对
		if (shopIdStr == null) {
			modelMap.put("success", false);
			modelMap.put("msg", "请传入商品分类id");
			return modelMap;
		}

		ProductCategory productCategoryCondition = new ProductCategory();
		productCategoryCondition.setShopId(Long.parseLong(shopIdStr));
		List<ProductCategory> productCategoryList = productCategoryService
				.queryForProductCategory(productCategoryCondition);
		modelMap.put("productCategoryList", productCategoryList);
		modelMap.put("count", productCategoryList.size());
		modelMap.put("msg", "商品分类分类列表");
		return modelMap;
	}

	/**
	 * 删除商品分类
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/productcategoryremove")
	@ResponseBody
	private Map<String, Object> removeProductCategoryById(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<>();
		String productCategoryId = request.getParameter("productCategoryId");
		// 前台传入参数不对
		if (productCategoryId == null) {
			modelMap.put("success", false);
			modelMap.put("msg", "删除失败，请传入商品分类id");
			return modelMap;
		}

		int res = productCategoryService.delete(Integer.parseInt(productCategoryId));
		// 判断删除是否成功
		if (res <= 0) {
			// 失败
			modelMap.put("success", false);
			modelMap.put("msg", "删除失败");
		} else {
			// 成功
			modelMap.put("success", true);
			modelMap.put("msg", "删除成功");
		}

		return modelMap;
	}

	/**
	 * 批量添加商品类别
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/productcategoryadd",method=RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> batchInsertProductCategoryById(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<>();
		String productCategoryInfos = request.getParameter("productCategoryInfo");
		System.out.println("=====================");
		System.out.println(productCategoryInfos);
		if (productCategoryInfos == null) {
			modelMap.put("success", false);
			modelMap.put("msg", "请输入添加的商品分类信息");
			return modelMap;
		}

		try {
			@SuppressWarnings("unchecked")
			List<ProductCategory> productCategoryList = mapper.readValue(productCategoryInfos, List.class);
			for (ProductCategory productCategory : productCategoryList) {
				System.out.println(productCategory.getProductCategoryName()+"=="+productCategory.getShopId());
			}
			int res = productCategoryService.batchInsert(productCategoryList);
			// 判断删除是否成功
			if (res <= 0) {
				// 失败
				modelMap.put("success", false);
				modelMap.put("msg", "添加失败");
			} else {
				// 成功
				modelMap.put("success", true);
				modelMap.put("msg", "添加成功");
			}
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("msg", e.getMessage());
		}

		return modelMap;
	}
}
